<template>
  <view v-if="goodsInfo.goods_name">
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item, i) in goodsInfo.pics" :key="item.pics_id" @click="preview(i)">
        <image :src="item.pics_mid_url"></image>
      </swiper-item>
    </swiper>

    <!-- 商品描述 -->
    <view class="goods-discription">
      <view class="goods-price">￥{{goodsInfo.goods_price}}</view>
      <view class="goods-discription-body">
        <text class="goods-title">{{goodsInfo.goods_name}}</text>
        <view class="goods-collect">
          <uni-icons type="star" size="18"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="kd">快递：全国包邮</view>
    </view>
    
    <!-- 商品详情 -->
    <view class="goodsDescription">
      <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
    </view>
    
    <!-- 商品导航 -->
    <view class="goods_nav">
      <uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {mapState, mapMutations, mapGetters} from 'vuex'
  export default {
    data() {
      return {
        goodsInfo: [],
        options: [{
        			icon: 'shop',
        			text: '店铺',
        			infoBackgroundColor:'#007aff',
        			infoColor:"red"
        		}, {
        			icon: 'cart',
        			text: '购物车',
        			info: 0
        		}],
        	    buttonGroup: [{
        	      text: '加入购物车',
        	      backgroundColor: '#ff0000',
        	      color: '#fff'
        	    },
        	    {
        	      text: '立即购买',
        	      backgroundColor: '#ffa200',
        	      color: '#fff'
        	    }
        	    ]
      };
    },

    onLoad(option) {
      this.getGoodsInfoList(option.goods_id)
    },

    methods: {
      // 映射 VueX 的方法
      ...mapMutations('m_cart', ['addGoodsCart']),
      
      // 获取商品详情页数据
      async getGoodsInfoList(goods_id) {
        const {data: res} = await uni.$http.get('/api/public/v1/goods/detail', {goods_id})
        if (res.meta.status !== 200) return uni.showMsg('获取商品详细信息失败')
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display: block;"')
        res.message.goods_introduce = res.message.goods_introduce.replace(/webp /g, 'jpg')
        this.goodsInfo = res.message
      },

      // 播放大图
      preview(i) {
        uni.previewImage({
          current: i,
          urls: this.goodsInfo.pics.map(x => x.pics_big_url)
        })
      },

      // 跳转到购物车
      onClick(e) {
        if(e.content.text == '购物车') {
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      },

      // 添加购物车或者购买商品
      buttonClick(e) {
        if(e.content.text == '加入购物车') {
          const goods = {
            goods_id: this.goodsInfo.goods_id,
            goods_name: this.goodsInfo.goods_name,
            goods_price: this.goodsInfo.goods_price,
            goods_count: 1,
            goods_small_logo: this.goodsInfo.goods_small_logo,
            goods_state: true
          }
          this.addGoodsCart(goods)
        }
      }
    },

    computed: {
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['total'])
    },
    
    watch: {
      total: {
        handler(newVal) {
          const findResult = this.options.find(x => x.text == '购物车')
          if(findResult) {
            findResult.info = newVal
          }
        },
        // 当页面刚加载时就会被触发
        immediate: true
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-discription {
    padding: 10rpx 0;

    .goods-price {
      color: #C00000;
      font-size: 36rpx;
      margin: 10rpx;
    }

    .goods-discription-body {
      display: flex;

      .goods-title {
        font-size: 26rpx;
        margin-right: 10rpx;
      }

      .goods-collect {
        width: 150rpx;
        color: gray;
        font-size: 24rpx;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1px solid #efefef;
      }
    }

    .kd {
      margin: 10rpx 0;
      font-size: 24rpx;
      color: gray;
    }
  }

  .goods_nav {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }

</style>
